package com.chinamobile.maps.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.chinamobile.maps.dao.IAccesslogsDAO;
import com.chinamobile.maps.orm.Accesslogs;
import com.chinamobile.maps.util.JsonUtil;
import com.chinamobile.maps.util.Struts2Util;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: AccesslogsAction
 * @Description: TODO
 * @author: frankwong
 * @date: 2012-10-21 下午4:31:34
 * 
 */
public class AccesslogsAction extends ActionSupport {

	private IAccesslogsDAO accesslogsDao = null;

	public void getLogsList() {
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			int pageNumber = Integer.parseInt((request
					.getParameter("pageNumber")));
			int pageSize = Integer.parseInt((request.getParameter("pageSize")));
			List<Accesslogs> accesslogsList = this.accesslogsDao
					.getAccesslogs();
			int count;
			if (accesslogsList == null) {
				out.print("{\"result\":false}");
				return;
			} else {
				count = accesslogsList.size();
				List<Accesslogs> accesslogsResult = new ArrayList<Accesslogs>();
				for (int i = pageSize * (pageNumber - 1); i < count
						&& i < pageSize * pageNumber; i++) {
					accesslogsResult.add(accesslogsList.get(i));
				}
				Map<String, Object> jsonMap = new HashMap<String, Object>();
				jsonMap.put("total", count);
				jsonMap.put("rows", accesslogsResult);
				JSONObject jsonObject = JSONObject.fromObject(jsonMap,
						JsonUtil.jsonConfig);
				out.print(jsonObject.toString());
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}

	/**
	 * @return the accesslogsDao
	 */
	public IAccesslogsDAO getAccesslogsDao() {
		return accesslogsDao;
	}

	/**
	 * @param accesslogsDao
	 *            the accesslogsDao to set
	 */
	public void setAccesslogsDao(IAccesslogsDAO accesslogsDao) {
		this.accesslogsDao = accesslogsDao;
	}

	public void downloadLog() throws IOException {
		HttpServletResponse response = Struts2Util.getResponse();
		HttpServletRequest request = Struts2Util.getRequest();
		String selectedDate = String.valueOf((request
				.getParameter("selectedDate")));
		String targetFileName = "application-" + selectedDate + ".log";
		File file = new File("c:/MobileMaps/log/" + targetFileName);
		/* 如果文件存在 */
		if (file.exists()) {
			String filename = URLEncoder.encode(file.getName(), "UTF-8");
			response.reset();
			response.setContentType("text/plain");
			response.setCharacterEncoding("UTF-8");
			response.addHeader("Content-Disposition", "attachment; filename=\""
					+ filename + "\"");
			int fileLength = (int) file.length();
			response.setContentLength(fileLength);
			/* 如果文件长度大于0 */
			if (fileLength != 0) {
				/* 创建输入流 */
				InputStream inStream = new FileInputStream(file);
				byte[] buf = new byte[4096];
				/* 创建输出流 */
				ServletOutputStream servletOS = response.getOutputStream();
				int readLength;
				while (((readLength = inStream.read(buf)) != -1)) {
					servletOS.write(buf, 0, readLength);
				}
				inStream.close();
				servletOS.flush();
				servletOS.close();
			}
		} else {
			String filename = URLEncoder.encode(file.getName(), "UTF-8");
			response.reset();
			response.setContentType("text/plain");
			response.setCharacterEncoding("UTF-8");
			response.addHeader("Content-Disposition", "attachment; filename=\""
					+ filename + "\"");
			response.setContentLength(0);
			/* 创建输出流 */
			byte[] buff = new byte[0];
			ServletOutputStream servletOS = response.getOutputStream();
			servletOS.write(buff, 0, 0);
			servletOS.flush();
			servletOS.close();
		}
	}
}
